import json

from django.db import models

from accounts.models import RegularUser, CultureAdmin


class Tourist_Spot_Type(models.Model):
    """ 景点类型 """
    name = models.CharField(max_length=16, verbose_name="景点类型")


class TouristSpot(models.Model):
    STATUS_CHOICES = (
        ('pending', '待审核'),
        ('approved', '已通过'),
        ('rejected', '已拒绝'),
    )
    LEVEL_CHOICES = (
        (1, "一星"),
        (2, "二星"),
        (3, "三星"),
        (4, "四星"),
        (5, "五星")
    )

    name = models.CharField(max_length=100, verbose_name='景点名称')
    type = models.ForeignKey(to="Tourist_Spot_Type", to_field="id", on_delete=models.CASCADE)
    wl = models.ForeignKey("accounts.CultureAdmin", to_field="id", verbose_name="所属文旅", on_delete=models.CASCADE)
    ticket_price = models.FloatField(verbose_name="门票价格")
    time = models.CharField(verbose_name="开放时间", max_length=32)
    description = models.TextField(verbose_name='景点描述')
    location = models.CharField(max_length=200, verbose_name='景点位置')
    cover_image = models.ImageField(upload_to='spots/covers/', verbose_name='封面图片')
    latitude = models.DecimalField(max_digits=9, decimal_places=6)
    longitude = models.DecimalField(max_digits=9, decimal_places=6)
    level = models.SmallIntegerField(verbose_name="星级", choices=LEVEL_CHOICES, default=1)
    status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='pending', verbose_name='审核状态')
    rejection_reason = models.TextField(blank=True, null=True, verbose_name='拒绝原因')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name = '旅游景点'
        verbose_name_plural = '景点管理'
        ordering = ['-created_at']


class Tag(models.Model):
    name = models.CharField(max_length=50)


class TouristArticle(models.Model):
    SEASON_CHOICES = (
        ('spring', "春季"),
        ('summer', "夏季"),
        ('autumn', "秋季"),
        ('winter', "冬季")
    )
    TYPE_CHOICES = (
        ('free', "自由行"),
        ('group', "跟团游"),
        ('drive', "自驾游"),
        ('backpack', "背包客"),
        ('family', "亲子游"),
        ('honeymoon', "蜜月旅行")
    )
    STATE_CHOICES = (
        ('asia', "亚洲"),
        ('europe', "欧洲"),
        ('north_america', "北美洲"),
        ('south_america', "南美洲"),
        ('africa', "非洲"),
        ('oceania', "大洋洲")
    )
    TAG_CHOICES = [
        ('free', '自由行'),
        ('drive', '自驾游'),
        ('group', '跟团游'),
        ('backpack', '背包客'),
        ('family', '亲子游'),
        ('honeymoon', '蜜月旅行'),
        ('food', '美食之旅'),
        ('photo', '摄影之旅'),
        ('hiking', '徒步'),
        ('climbing', '登山'),
        ('island', '海岛度假'),
        ('city_explore', '城市探索'),
    ]

    author = models.ForeignKey(
        RegularUser,
        on_delete=models.CASCADE,
        verbose_name="作者"
    )
    title = models.CharField(max_length=64, verbose_name="标题")
    days = models.IntegerField(verbose_name="旅行天数")
    season = models.CharField(max_length=16, verbose_name="旅行季节", choices=SEASON_CHOICES, default=1)
    type = models.CharField(max_length=16, verbose_name="旅行类型", choices=TYPE_CHOICES, default=1)
    state = models.CharField(max_length=16, verbose_name="所在大洲", default='asia')
    country = models.CharField(max_length=32, verbose_name="旅行国家")
    city = models.CharField(max_length=32, verbose_name="旅行城市")
    destination = models.CharField(max_length=64, verbose_name="具体目的地")
    date = models.DateField(verbose_name="旅行时间")
    tags = models.ManyToManyField(Tag, blank=True,
                                  verbose_name="标签",
                                  help_text="多个标签用逗号分隔",
                                  )
    introduction = models.CharField(verbose_name="游记简介", max_length=256)
    content = models.CharField(max_length=2048, verbose_name="详细内容")
    created_at = models.DateTimeField(auto_now_add=True, blank=True, null=True)

    cover_path = models.CharField(verbose_name="封面图片路径", null=True, max_length=64)
    content_paths = models.CharField(verbose_name="内容图片路径", null=True, max_length=64)

    view_count = models.IntegerField(verbose_name="浏览量", null=True, default=0)
    comment_count = models.IntegerField(verbose_name="评论量", null=True, default=0)
    like_count = models.IntegerField(verbose_name="点赞量", null=True, default=0)
    collect_count = models.IntegerField(verbose_name="收藏量", null=True, default=0)

    def get_tags_list(self):
        return [tag.strip() for tag in self.tags.split(',')] if self.tags else []


class TouristArticleComment(models.Model):
    """ 文章评论 """
    article = models.ForeignKey(to=TouristArticle, verbose_name="所属文章", on_delete=models.CASCADE)
    user = models.ForeignKey(verbose_name="评论用户", to="accounts.RegularUser", on_delete=models.CASCADE)
    comment_at = models.DateTimeField(auto_now_add=True, blank=True, null=True)
    like_count = models.IntegerField(verbose_name="点赞量", null=True, default=0)
